package com.xzx.mr.compress;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.compress.CompressionCodec;
import org.apache.hadoop.io.compress.CompressionCodecFactory;
import org.apache.hadoop.io.compress.CompressionInputStream;
import org.apache.hadoop.io.compress.CompressionOutputStream;
import org.apache.hadoop.util.ReflectionUtils;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;


/**
 * @author xinzhixuan
 * @version V1.0
 * @date 2019/7/31 22:22
 */
public class TestCompress {
    public static void main(String[] args) throws Exception {
        // 压缩
//        compress("d:/xzx.txt", "org.apache.hadoop.io.compress.GzipCodec");
        // 解压
        decompress("d:/xzx.txt.gz");
    }

    private static void decompress(String fileName) throws IOException {
        CompressionCodecFactory factory =  new CompressionCodecFactory(new Configuration());
        CompressionCodec codec = factory.getCodec(new Path(fileName));
        CompressionInputStream inputStream = codec.createInputStream(new FileInputStream(fileName));

        FileOutputStream outputStream = new FileOutputStream(fileName.substring(0,
                fileName.indexOf(codec.getDefaultExtension())) );

        IOUtils.copyBytes(inputStream, outputStream, 1024 * 1024);

        IOUtils.closeStream(outputStream);
        IOUtils.closeStream(inputStream);
    }

    private static void compress(String fileName, String codecName) throws IOException, ClassNotFoundException {
        FileInputStream fileInputStream = new FileInputStream(fileName);

        Class aClass = Class.forName(codecName);
        CompressionCodec compressionCodec = (CompressionCodec) ReflectionUtils.newInstance(aClass, new Configuration());
        CompressionOutputStream outputStream = compressionCodec
                .createOutputStream(new FileOutputStream(fileName + compressionCodec.getDefaultExtension()));

        IOUtils.copyBytes(fileInputStream, outputStream, 1024 * 1024);

        IOUtils.closeStream(outputStream);
        IOUtils.closeStream(fileInputStream);
    }
}
